package com.fdd.furns.service.impl;

import com.fdd.furns.dao.FurnDAO;
import com.fdd.furns.dao.impl.FurnDAOImpl;
import com.fdd.furns.pojo.Furn;
import com.fdd.furns.pojo.Page;
import com.fdd.furns.service.FurnService;

import java.util.Collections;
import java.util.List;

public class FurnServiceImpl implements FurnService {
    FurnDAO furnDAO = new FurnDAOImpl();

    @Override
    public List<Furn> queryFurns() {
        return furnDAO.queryFurns();
    }

    @Override
    public int addFurn(Furn furn) {
        return furnDAO.addFurn(furn);
    }

    @Override
    public int deleteFurnById(int id) {
        return furnDAO.deleteFurnById(id);
    }

    @Override
    public Furn queryFurnById(int id) {
        return furnDAO.queryFurnById(id);
    }

    @Override
    public int updateFurn(Furn furn) {
        return furnDAO.updateFurn(furn);
    }

    @Override
    public Page<Furn> page(int pageNo, int pageSize) {
        Page page = new Page();
        //前台传的
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        //后台算的
        page.setItems(furnDAO.getPageItems(pageNo, pageSize));
        page.setTotalRow(furnDAO.queryTotalRow());
        page.setPageTotalCount(page.getTotalRow() / page.getPageSize());
        if (page.getTotalRow() % page.getPageSize() != 0) {
            page.setPageTotalCount(page.getPageTotalCount() + 1);
        }
        return page;
    }

    @Override
    public int queryTotalRow() {
        return 0;
    }

    @Override
    public Page<Furn> pageByName(int pageNo, int pageSize, String name) {
        Page page = new Page();
        //前台传的
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);

        //后台算的
        page.setItems(furnDAO.getPageItemsByName(pageNo, pageSize, name));
        page.setTotalRow(furnDAO.queryTotalRowByName(name));
        page.setPageTotalCount(page.getTotalRow() / page.getPageSize());
        if (page.getTotalRow() % page.getPageSize() != 0) {
            page.setPageTotalCount(page.getPageTotalCount() + 1);
        }
        return page;
    }


    @Override
    public int queryTotalRowByName(String name) {
        return furnDAO.queryTotalRowByName(name);
    }
}
